package com.hnas.sys.core.spring;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.core.util.DateUtil;
import com.hnas.sys.core.util.UserUtils;
import com.hnas.sys.dao.LogDAO;
import com.hnas.sys.dao.UserDAO;
import com.hnas.sys.orm.LogInfo;
import com.hnas.sys.orm.User;
import com.hnas.sys.service.system.ResourceService;
import com.hnas.sys.service.system.dto.DTOUser;

public class MyAuthenticationSuccessHandler extends
		SavedRequestAwareAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
	@Autowired
	private LogDAO logDAO;
//	@Autowired
//	private ObjectTypeDAO objectTypeDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private ResourceService resourceService;
	
	@Transactional(propagation=Propagation.REQUIRED,
            rollbackFor=Exception.class)
	public void onAuthenticationSuccess(HttpServletRequest request,  
            HttpServletResponse response, Authentication authentication)  
            throws ServletException, IOException {  
        //这里填写自己要执行的操作 如用户的登录时间、ip等等。  
		super.onAuthenticationSuccess(request,response,authentication);
		User operator = this.userDAO.findByUserName(authentication.getName());
//		ObjectType object = this.objectTypeDAO.getObjectByCode("User");		
//		if (null==object)
//			throw new CommonException("找不到实体对象，请与系统管理员联系！");
		LogInfo log = new LogInfo();
		log.setOperator(operator);
		log.setObjectType(User.class.getSimpleName());
		log.setObjectId(operator.getId());
		log.setLogInfo("登录，ip:"+request.getRemoteAddr());
		log.setOperateTime(DateUtil.dateTimeToString(new Date()));
		log.setOperateType("login");
		this.logDAO.persist(log);
		//清除登录失败次数
//		operator.setLoginFailure(0);
		operator.setLastLogin(DateUtil.dateTimeToString(new Date()));
		this.userDAO.merge(operator);
		request.getSession().setAttribute("backend_menus", 
		this.resourceService.getUserMenus((DTOUser) authentication.getPrincipal()));
		request.getSession(true).removeAttribute(UserUtils.MIX_SMS_CODE_BACK);
		request.getSession(true).removeAttribute(UserUtils.MIX_IMG_CODE);
    }
	public LogDAO getLogDAO() {
		return logDAO;
	}
	public void setLogDAO(LogDAO logDAO) {
		this.logDAO = logDAO;
	}
//	public ObjectTypeDAO getObjectTypeDAO() {
//		return objectTypeDAO;
//	}
//	public void setObjectTypeDAO(ObjectTypeDAO objectTypeDAO) {
//		this.objectTypeDAO = objectTypeDAO;
//	}
	public UserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	} 

}
